package cn.zswltech.gruul.xxljob;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.zswltech.gruul.common.dal.dao.UserDOMapper;
import cn.zswltech.gruul.common.entity.UserDO;
import cn.zswltech.gruul.common.util.SpringBeanUtil;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jim
 * @version 1.0.0
 * @descripition: 云湖用户数据落库任务
 * @date 2024/2/21 15:19
 */
@Slf4j
public class YunHuUserFeatureTask implements Runnable {

    private final List<YunHuUser> yunHuUserList;

    public YunHuUserFeatureTask(List<YunHuUser> list) {
        this.yunHuUserList = list;
    }

    @Override
    public void run() {
        log.info("开始同步云湖用户数据");
        List<YunHuUser> needUpdateUsers;
        try {
            needUpdateUsers = SpringBeanUtil.getBean(HttpYunHuInfoSync.class).getNeedUpdateUsers(yunHuUserList);
            log.info("需要新增用户数据量：{}", needUpdateUsers.size());
            batchUpdateYunHuUser(needUpdateUsers);
        } catch (Exception e) {
            log.error("云湖数据落库失败", e);
            failureHandle();
            return;
        }
        List<UserDO> userDOList;
        try {
            userDOList = SpringBeanUtil.getBean(HttpYunHuInfoSync.class).yunHuUserConvert(needUpdateUsers);
            log.info("云湖用户转换结果：{}", userDOList);
        } catch (Exception e) {
            log.error("用户转换失败", e);
            failureHandle();
            return;
        }
        try {
            batchUpdateUser(userDOList);
        } catch (Exception e) {
            log.error("用户落库失败",e);
            failureHandle();
        }
    }

    /**
     * 批量更新用户
     */
    private void batchUpdateUser(List<UserDO> userDOList) {
        if (ObjectUtil.isEmpty(userDOList)){
            return;
        }
        List<String> accountList = userDOList.stream().map(UserDO::getAccount).collect(Collectors.toList());
        List<UserDO> userDOS = SpringBeanUtil.getBean(UserDOMapper.class).selectByExample(Example.builder(UserDO.class).andWhere(Sqls.custom().andIn("account", accountList)).build());
        log.info("已存在用户表数据：{}条", userDOS.size());
        if (ObjectUtil.isNotEmpty(userDOS)){
            List<String> existAccounts = userDOS.stream().map(UserDO::getAccount).collect(Collectors.toList());
            userDOList = userDOList.stream().filter(item->!existAccounts.contains(item.getAccount())).collect(Collectors.toList());
            log.info("过滤已存在用户后，待新增用户数：{}条", userDOList.size());
        }
        SpringBeanUtil.getBean(UserDOMapper.class).insertList(userDOList);
        log.info("{}：更新用户数：{}", DateUtil.now(),userDOList.size());
    }


    /**
     * 批量更新用户
     */
    private void batchUpdateYunHuUser(List<YunHuUser> yunHuUsers){
        List<String> empIds = yunHuUsers.stream().map(YunHuUser::getEmpId).collect(Collectors.toList());
        SpringBeanUtil.getBean(YunHuUserMapper.class).batchDeleteByPrimaryKeys(empIds);
        SpringBeanUtil.getBean(YunHuUserMapper.class).batchSave(yunHuUsers);
    }

    public void failureHandle(){
        log.info("同步失败用户落库待后续处理,同步失败用户集合：{}",yunHuUserList.stream().map(YunHuUser::getEmpId));
    }


}
